package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.util.Globals;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.mybatis.callcenter.entity.WorkOrderInfo;
import cn.com.nes.mybatis.callcenter.mapper.CallWorkOrderInfoMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.workOrder.GetAllWorkOrderPageVO;
import cn.com.nes.site.service.workOrder.CallWorkOrderInfoService;
import cn.com.nes.site.service.workOrder.dto.CustomerRefusalWorkOrderInfoDTO;
import cn.com.nes.site.service.workOrder.dto.GetAllWorkOrderPageDTO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderInstallRecordDTO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CallWorkOrderInfoServiceImpl implements CallWorkOrderInfoService {
    @Resource
    private CallWorkOrderInfoMapper callWorkOrderInfoMapper;


    @Override
    public List<GetAllWorkOrderPageDTO> queryOrderPage(Page<GetAllWorkOrderPageDTO> page, GetAllWorkOrderPageVO vo) {
        return callWorkOrderInfoMapper.queryOrderPage(page,vo);
    }

    @Override
    public WorkOrderInfo selectByPrimaryKey(Long workOrderId) {
        return callWorkOrderInfoMapper.selectById(workOrderId);
    }

    @Override
    public Integer updateByPrimaryKey(WorkOrderInfo workOrderInfo) {
        return callWorkOrderInfoMapper.updateById(workOrderInfo);
    }

    /**
     * 安装工单 - 拒单
     */
    @Override
    public Integer rejectionOrderInstall(WorkOrderInfo workOrderInfo) {
        return callWorkOrderInfoMapper.rejectionOrderInstall(workOrderInfo);
    }


    @Override
    public PageData getWorkOrderInstallTimeAxis(Long workOrderId) {
        List<WorkOrderInstallRecordDTO> workOrderRecordList = callWorkOrderInfoMapper.getWorkOrderInstallTimeAxis(workOrderId);

        if (CollUtil.isNotEmpty(workOrderRecordList)) {
            for (WorkOrderInstallRecordDTO dto : workOrderRecordList) {
                // 处理图片信息
                if (StrUtil.isNotBlank(dto.getImages())) {
                    String[] images = dto.getImages().split(";");
                    List<String> imageList = new ArrayList<>();
                    for (String image : images) {
                        String imagesPath = OssUtil.getImgSignURL(image, 1, Globals.bucketName_call);
                        imageList.add(imagesPath);
                    }
                    dto.setImageList(imageList);
                }
                if (StrUtil.isBlank(dto.getUserName())) {
                    dto.setUserName("移动端用户");
                }
                // 处理时间字段
                if (ObjectUtil.isNotNull(dto.getCreateTimeStamp())) {
                    dto.setCreateTime(new Date(dto.getCreateTimeStamp()));
                }
            }
        }
        // 处理过期状态 - 等待工单过期时间定了再考虑工单的过期状态处理
        return PageData.ok("workOrderRecordList", workOrderRecordList);
    }

    @Override
    public Long getHouseIdByWorkOrderId(Long workOrderId) {
        return callWorkOrderInfoMapper.getHouseIdByWorkOrderId(workOrderId);
    }

    @Override
    public List<CustomerRefusalWorkOrderInfoDTO> getCustomerRefusalWorkOrderInfo(List<String> customerIdList, Set<Long> workOrderIdSet) {
        if (CollectionUtil.isEmpty(customerIdList) || CollectionUtil.isEmpty(workOrderIdSet)) {
            return Collections.emptyList();
        }
        return callWorkOrderInfoMapper.getCustomerRefusalWorkOrderInfo(customerIdList, workOrderIdSet);
    }
    @Override
    public   WorkOrderInfo getWorkOrderByPworkOrderId(Long pWorkOrderId){
        return callWorkOrderInfoMapper.getWorkOrderByPworkOrderId(pWorkOrderId);

    }
}
